﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Dataglot;

namespace IrishSettings
{
    public class SelectApplicationVersions : GenericCommand
    {
        public SelectApplicationVersions(DataFactory factory):base(factory)
        {
            const string sql = @"select version from {$ns}_settings_applications WHERE
    application= @application 
    group by version ";
            ComposeSql(sql);
            AddParameter("@application", DbType.String);
        }

        public List<Version> Select(
            string application
            )
        {
            if (string.IsNullOrEmpty(application))
                throw new ArgumentException("Missing application name", "application");
            
            command.Parameters["@application"].Value = application;
            List<Version> versions = new List<Version>();
            using(DbDataReader reader= ExecuteReader(CommandBehavior.Default))
            {
                if(reader.HasRows)
                {
                    while(reader.Read())
                    {
                        Version v = new Version(reader.GetString(0));
                        versions.Add(v);
                    }
                }
                else
                {
                    return versions;
                }
            }
            return versions;
        }
    }
}
